﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class BoltService
    {
        internal static BoltOut BoltResult(BoltIn boltIn)
        {
            BoltOut boltOut = new();
            boltOut.MGCS_SectionalArea = Bolt_Cal_SectionalArea(boltIn.BoltDiameter);
            boltOut.MGCS_Weight = Bolt_Cal_Weight(boltIn.BoltDiameter);
            boltOut.MGCS_Strength = Bolt_Cal_Strength(boltIn.BoltMaterial);

            boltOut.MGLL_DesignValue = Bolt_Cal_TensionDesign(boltIn.BoltDiameter, boltIn.BoltMaterial);
            boltOut.MGLL_StandardValue = Bolt_Cal_TensionStandard(boltIn.BoltDiameter, boltIn.BoltMaterial);

            double length1 = Bolt_Cal_Length1(boltIn.BoltDiameter, boltIn.BoltMaterial, boltIn.SafetyValue, boltIn.WY_Value, boltIn.MGD_Value, boltIn.BoreholeDiameter);
            double length2 = Bolt_Cal_Length2(boltIn.BoltDiameter, boltIn.BoltMaterial, boltIn.FmsValue, double.Parse((boltIn.GJ_Radical == "5根以上") ? "5" : boltIn.GJ_Radical), boltIn.GJ_Factor);

            if (length1 > length2)
            {
                boltOut.MGCD_DC = length1;//length1.ToString() + "  √";
                boltOut.MGCD_JT = length2;
            }
            else
            {
                boltOut.MGCD_DC = length1;
                boltOut.MGCD_JT = length2;//length2.ToString() + "  √";
            }

            boltOut.MG_MinLength = Bolt_Cal_LengthDesign(length1, length2);

            return boltOut;
        }
        //计算_锚杆截面积
        private static double Bolt_Cal_SectionalArea(double diameter)
        {
            double result = 0;

            result = Math.Round((Math.Pow(diameter / 2, 2) * Math.PI), 2);

            return result;
        }
        //计算_锚杆重量
        private static double Bolt_Cal_Weight(double diameter)
        {
            double result = 0;

            result = Math.Round((Math.Pow(diameter / 2, 2) * Math.PI) * 7.85 / 1000, 2);

            return result;
        }
        //计算_锚杆杆体抗拉强度
        private static double Bolt_Cal_Strength(double Bolt_Mpa)
        {
            double result = 0;
            switch (Bolt_Mpa)
            {
                case 300:
                    result = 270;
                    break;
                case 400:
                    result = 360;
                    break;
                case 500:
                    result = 435;
                    break;
                default:
                    result = Bolt_Mpa - 30;
                    break;
            }

            return result;
        }
        //计算_锚杆拉力设计值
        private static double Bolt_Cal_TensionDesign(double diameter, double Bolt_Mpa)
        {
            double result = 0;
            result = Math.Round(Bolt_Cal_SectionalArea(diameter) * Bolt_Cal_Strength(Bolt_Mpa) / 1000, 1);
            return result;
        }
        //计算_锚杆拉力标准值
        private static double Bolt_Cal_TensionStandard(double diameter, double Bolt_Mpa)
        {
            double result = 0;
            result = Math.Round(Bolt_Cal_TensionDesign(diameter, Bolt_Mpa) / 1.35 / 1.1, 1);
            return result;
        }

        //计算_锚杆锚固段长度（按锚固段注浆体与地层间极限粘结强度计算）
        private static double Bolt_Cal_Length1(double diameter, double Bolt_Mpa, double K, double fmg, double ψ, double holeDiameter)
        {
            double result = 0;
            result = Math.Round(Bolt_Cal_TensionDesign(diameter, Bolt_Mpa) * K / (fmg * ψ * Math.PI * holeDiameter), 2);
            return result;
        }
        //计算_锚杆锚固段长度（按锚固段注浆体与筋体间粘结强度计算）
        private static double Bolt_Cal_Length2(double diameter, double Bolt_Mpa, double fms, double number, double ξ)
        {
            double result = 0;
            result = Math.Round(Bolt_Cal_TensionDesign(diameter, Bolt_Mpa) / (fms * number * Math.PI * diameter * ξ), 2);
            return result;
        }
        //计算_建议选择的锚杆最小长度
        private static double Bolt_Cal_LengthDesign(double Length1, double Length2)
        {
            double result = 0;
            if (Length2 > Length1)
            {
                result = Length2 + 0.3;
            }
            else
            {
                result = Length1 + 0.5;
            }

            return result;
        }
    }
}
